<?php
include_once "header.php";

//echo $_SESSION['unit'];

if($_SESSION['kewenangan']=="operator" || $_SESSION['kewenangan']=="kakanwil"){
		$daftar_surat_masuk = new Daftar_surat_masuk();
		$surat_masuk = $daftar_surat_masuk->getAll();
	}

	if($_SESSION['kewenangan']=="kabu/kabid" || $_SESSION['kewenangan']=="kasubag/kasi" || $_SESSION['kewenangan']=="pelaksana"){
		$daftar_surat_masuk = new Daftar_surat_masuk();	
		$surat_masuk = $daftar_surat_masuk->getByUnitDisposisi($_SESSION['unit']);
	}

if(isset($_POST['cari_sm']) && $_POST['hal']!="" && $_POST['tgl']=="" ){
	
	if($_SESSION['kewenangan']=="operator" || $_SESSION['kewenangan']=="kakanwil"){
		$daftar_surat_masuk = new Daftar_surat_masuk();
		$cari=explode(" ",$_POST['hal']);
		$surat_masuk = $daftar_surat_masuk->searchByHal($cari);
	}

	if($_SESSION['kewenangan']=="kabu/kabid" || $_SESSION['kewenangan']=="kasubag/kasi" || $_SESSION['kewenangan']=="pelaksana"){
		$daftar_surat_masuk = new Daftar_surat_masuk();	
		$cari=explode(" ",$_POST['hal']);
		$surat_masuk = $daftar_surat_masuk->searchByHalUnit($cari,$_SESSION['unit']);
	}

}

if(isset($_POST['cari_sm']) && $_POST['hal']!="" && $_POST['tgl']!="" ){
	
	if($_SESSION['kewenangan']=="operator" || $_SESSION['kewenangan']=="kakanwil"){
		$daftar_surat_masuk = new Daftar_surat_masuk();
		$cari=explode(" ",$_POST['hal']);
		$surat_masuk = $daftar_surat_masuk->searchByHalTgl($cari,$_POST['tgl']);
	}

	if($_SESSION['kewenangan']=="kabu/kabid" || $_SESSION['kewenangan']=="kasubag/kasi" || $_SESSION['kewenangan']=="pelaksana"){
		$daftar_surat_masuk = new Daftar_surat_masuk();	
		$cari=explode(" ",$_POST['hal']);
		$surat_masuk = $daftar_surat_masuk->searchByHalUnit($cari,$_POST['tgl'],$_SESSION['unit']);
	}

}

if(isset($_POST['cari_sm']) && $_POST['hal']=="" && $_POST['tgl']!="" ){
	
	if($_SESSION['kewenangan']=="operator" || $_SESSION['kewenangan']=="kakanwil"){
		$daftar_surat_masuk = new Daftar_surat_masuk();
		$cari=explode(" ",$_POST['hal']);
		$surat_masuk = $daftar_surat_masuk->searchByTgl($_POST['tgl']);
	}

	if($_SESSION['kewenangan']=="kabu/kabid" || $_SESSION['kewenangan']=="kasubag/kasi" || $_SESSION['kewenangan']=="pelaksana"){
		$daftar_surat_masuk = new Daftar_surat_masuk();	
		$cari=explode(" ",$_POST['hal']);
		$surat_masuk = $daftar_surat_masuk->searchByTglUnit($_POST['tgl'],$_SESSION['unit']);
	}

}


if($_SESSION['kewenangan']=="administrator"){
	header("Location:beranda.php");
}


?>

<div id="container">
	<div id="maincont">
		<?php include_once "menu_samping.php"; ?>
		<div id="mainright">
			<div id="title">
			Surat Masuk
			</div>
			<div id="body">
			<br>
			Kata Kunci: <br/>			
			<form name="form" method="POST" action="surat_masuk.php">
			<input type="hidden" name="cari_sm">
			<input type="text" name="hal" placeholder="hal" value="<?php if(isset($_POST['hal'])) echo $_POST['hal']; ?>"> &nbsp
			<input type="text" name="tgl" placeholder="tanggal" id="tgl" value="<?php if(isset($_POST['tgl'])) echo $_POST['tgl']; ?>"> &nbsp &nbsp &nbsp
			<input type="submit" name="submit" value="Submit" onclick="return validasi();">
			<input type="reset" name="reset" value="Reset">
			</form>
			<br/>
			
			
			
			<table id="table2" cellspacing=0 cellpadding=4 width=100%>
				<tr>
					<th>No.<br/>Agenda</th>
					<th>Nomor</th>
					<th>Tanggal</th>
					<th>Sifat</th>
					<th>Hal</th>
					<th>Asal</th>
					<th>Waktu Terima</th>
					<th>Posisi Terakhir</th>
					<th></th>
				</tr>
				<?php
				foreach($surat_masuk as $surat){			
				?>
				<tr>
					<td><?php echo $surat->getNo_agenda(); ?></td>
					<td><?php echo $surat->getNomor(); ?></td>
					<td><?php echo $surat->getTgl(); ?></td>
					<td><?php
					$daftar_sifat = new Daftar_sifat_surat();
					$sifat_surat = $daftar_sifat->getById($surat->getSifat());
					echo $sifat_surat->getSifat(); 
					?></td>
					<td><?php echo $surat->getHal(); ?></td>
					<td><?php echo $surat->getAsal(); ?></td>
					<td><?php echo $surat->getWaktu_terima(); ?></td>
					<td><?php 
					$daftar_posisi_sm = new Daftar_Posisi_surat();
					$posisi_sm = $daftar_posisi_sm->getLastPosisiSM($surat->getId());
					$daftar_user = new Daftar_user();
					if($posisi_sm == true){
						$user=$daftar_user->getByNip($posisi_sm->getPosisi_akhir());
						echo ucwords($user->getNama())."<br>(".$posisi_sm->getWaktu().")"; 
					} else {
						$user=$daftar_user->getByNip($surat->getPenerima());
						echo ucwords($user->getNama())."<br>(".$surat->getWaktu_terima().")";
					}
					?></td>
					<td>
					<?php 
					echo "<a href=\"detil_sm.php?id=".$surat->getId()."\">";
					echo "<img src=\"images/detail-icon.png\" alt=\"detil\" title=\"detil\" width=15px height=15px/></a> ";
					echo " ";
					if($_SESSION['kewenangan']=="operator"){
						echo "<a href=\"hapus_sm.php?id=".$surat->getId()."\" onclick=\"return del()\">";
						echo "<img src=\"images/mail-delete-icon.png\" alt=\"hapus\" title=\"hapus\" width=15px height=15px/></a> ";
						
						echo "<a href=\"lembar_disposisi.php?id=".$surat->getId()."\" target=\"_blank\">";
						echo "<img src=\"images/print-icon.png\" alt=\"cetak disposisi\" title=\"cetak disposisi\" width=15px height=15px/></a></a> ";;
						echo " ";
					}
					?>
					</td>
				</tr>
				<?php } ?>
			</table>
			<?php
			if(empty($surat_masuk)){
				echo "<table cellspacing=0 cellpadding=4 width=100%>";
				echo "<tr>";
				echo "<td>";
					echo "Data tidak tersedia.";	
				echo "</td>";			
				echo "</tr>";
				echo "</table>";
			}
			?>
			<br>
			<input type="button" value="Rekam" onClick="window.location.href='rekam_sm.php'">
			
			
					
			</div>
        </div>
	</div>
</div>
<?php
	include_once "footer.php";
?>

<script type="text/javascript">
	function del(){
	if(confirm('Apakah Anda yakin akan menghapus surat ini?'))
	return true;
	else return false
	}
	
	$(function() { 
	$("#tgl").datepicker({dateFormat: "yy-mm-dd",
											buttonImage:'jquery/css/images/calendar.gif',
											buttonImageOnly: true,
											showOn: 'button'
	}); 
	});
	
	onload=function(){
	
		document.form.onreset=function(){
			window.location="surat_masuk.php";
		}
	}
	
	function validasi(){
	var x = document.form.hal.value;
	var y = document.form.tgl.value;
	if (x=="" && y==""){
		alert('Isikan kata kunci terlebih dahulu!');
		return false;
	}
	}
</script>
